home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / ooo / f_0020 / sbasic.jar / text / sbasic / common / 03090202.xml < prev    next >
Encoding:
Extensible Markup Language  |  2003-08-01  |  10.2 KB  |  68 lines

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <html><head><title>For...Next Statement [Runtime]</title><meta name="filename" content="text/sbasic/common/03090202"/><help:css-file-link xmlns:help="http://openoffice.org/2000/help"/><!--The CSS style header method for setting styles--><style type="text/css">
  3.  
  4.         p.P1{
  5.                 }
  6.         span.T1{
  7.                 font-weight:bold;}
  8.         </style></head><body>
  9.   
  10.   
  11.   <help:paragraphinfo state="E" xmlns:help="http://openoffice.org/2000/help"/><help:to-be-embedded Eid="fornext" xmlns:help="http://openoffice.org/2000/help">
  12.   <p class="Head1"><help:paragraphinfo state="U" number="1"/><help:key-word value="For; statement" tag="kw66556_1"/><help:key-word value="To; statement" tag="kw66556_4"/><help:key-word value="Step; statement" tag="kw66556_3"/><help:key-word value="Next; statement" tag="kw66556_2"/><help:link Id="66556">For...Next Statement [Runtime]</help:link></p>
  13.   <p class="Paragraph"><help:paragraphinfo state="U" number="2"/>Repeats the statements between the For...Next block a specified number of times.</p>
  14.   <help:paragraphinfo state="E"/></help:to-be-embedded>
  15.   <p class="Head2"><help:paragraphinfo state="U" number="3" xmlns:help="http://openoffice.org/2000/help"/>Syntax:</p>
  16.   <p class="Paragraph"><help:paragraphinfo state="U" number="4" xmlns:help="http://openoffice.org/2000/help"/>For counter=start To end [Step step]</p>
  17.   <p class="Paragraph"><help:paragraphinfo state="U" number="5" xmlns:help="http://openoffice.org/2000/help"/>statement block</p>
  18.   <p class="Paragraph"><help:paragraphinfo state="U" number="6" xmlns:help="http://openoffice.org/2000/help"/>[Exit For]</p>
  19.   <p class="Paragraph"><help:paragraphinfo state="U" number="7" xmlns:help="http://openoffice.org/2000/help"/>statement block</p>
  20.   <p class="Paragraph"><help:paragraphinfo state="U" number="8" xmlns:help="http://openoffice.org/2000/help"/>Next [counter]</p>
  21.   <p class="Head2"><help:paragraphinfo state="U" number="9" xmlns:help="http://openoffice.org/2000/help"/>Variables:</p>
  22.   <p class="Paragraph"><help:paragraphinfo state="U" number="10" xmlns:help="http://openoffice.org/2000/help"/><span class="T1">Counter:</span> Loop counter initially assigned the value to the right of the equal sign (start). Only numeric variables are valid. The loop counter increases or decreases according to the variable Step until End is passed.</p>
  23.   <p class="Paragraph"><help:paragraphinfo state="U" number="11" xmlns:help="http://openoffice.org/2000/help"/><span class="T1">Start:</span> Numeric variable that defines the initial value at the beginning of the loop.</p>
  24.   <p class="Paragraph"><help:paragraphinfo state="U" number="12" xmlns:help="http://openoffice.org/2000/help"/><span class="T1">End:</span> Numeric variable that defines the final value at the end of the loop.</p>
  25.   <p class="Paragraph"><help:paragraphinfo state="U" number="13" xmlns:help="http://openoffice.org/2000/help"/><span class="T1">Step:</span> Sets the value by which to increase or decrease the loop counter. If Step is not specified, the loop counter is incremented by 1. In this case, End must be greater than Start. If you want to decrease Counter, End must be less than Start, and Step must be assigned a negative value.</p>
  26.   <p class="Paragraph"><help:paragraphinfo state="U" number="14" xmlns:help="http://openoffice.org/2000/help"/>The <span class="T1">For...Next</span> loop repeats all of the statements in the loop for the number of times that is specified by the parameters.</p>
  27.   <p class="Paragraph"><help:paragraphinfo state="U" number="15" xmlns:help="http://openoffice.org/2000/help"/>As the counter variable is decreased, <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic checks if the end value has been reached. As soon as the counter passes the end value, the loop automatically ends.</p>
  28.   <p class="Paragraph"><help:paragraphinfo state="U" number="16" xmlns:help="http://openoffice.org/2000/help"/>It is possible to nest <span class="T1">For...Next</span> statements. If you do not specify a variable following the <span class="T1">Next</span> statement, <span class="T1">Next</span> automatically refers to the most recent <span class="T1">For</span> statement.</p>
  29.   <p class="Paragraph"><help:paragraphinfo state="U" number="17" xmlns:help="http://openoffice.org/2000/help"/>If you specify an increment of 0, the statements between <span class="T1">For</span> and <span class="T1">Next</span> are repeated continuously.</p>
  30.   <p class="Paragraph"><help:paragraphinfo state="U" number="18" xmlns:help="http://openoffice.org/2000/help"/>When counting down the counter variable, <help:productname xmlns:help="http://openoffice.org/2000/help">%PRODUCTNAME</help:productname> Basic checks for overflow or underflow. <text:s text:c="" xmlns:text="http://openoffice.org/2000/text"/>The loop ends when Counter exceeds End (positive Step value) or is less than End (negative Step value).</p>
  31.   <p class="Paragraph"><help:paragraphinfo state="U" number="19" xmlns:help="http://openoffice.org/2000/help"/>Use the <span class="T1">Exit For</span> statement to exit the loop unconditionally. This statement must be within a <span class="T1">For...Next</span> loop. Use the <span class="T1">If...Then</span> statement to test the exit condition as follows:</p>
  32.   <p class="Paragraph"><help:paragraphinfo state="U" number="20" xmlns:help="http://openoffice.org/2000/help"/>For...</p>
  33.   <p class="Paragraph"><help:paragraphinfo state="U" number="21" xmlns:help="http://openoffice.org/2000/help"/>statements</p>
  34.   <p class="Paragraph"><help:paragraphinfo state="U" number="22" xmlns:help="http://openoffice.org/2000/help"/>If condition = True Then Exit For</p>
  35.   <p class="Paragraph"><help:paragraphinfo state="U" number="23" xmlns:help="http://openoffice.org/2000/help"/>statements</p>
  36.   <p class="Paragraph"><help:paragraphinfo state="U" number="24" xmlns:help="http://openoffice.org/2000/help"/>Next</p>
  37.   <p class="Paragraph"><help:paragraphinfo state="U" number="25" xmlns:help="http://openoffice.org/2000/help"/>Note: In nested <span class="T1">For...Next</span> loops, if you exit a loop unconditionally with <span class="T1">Exit For</span>, only one loop is exited.</p>
  38.   <p class="Head2"><help:paragraphinfo state="U" number="26" xmlns:help="http://openoffice.org/2000/help"/>Example</p>
  39.   <p class="Paragraph"><help:paragraphinfo state="U" number="27" xmlns:help="http://openoffice.org/2000/help"/>The following example uses two nested loops to sort a string array with 10 elements ( sEntry() ), that are first filled with various contents:</p>
  40.   <p class="PropText"><help:paragraphinfo state="U" number="28" xmlns:help="http://openoffice.org/2000/help"/>Sub ExampleSort</p>
  41.   <p class="PropText"><help:paragraphinfo state="U" number="29" xmlns:help="http://openoffice.org/2000/help"/>Dim sEntry(9) As String</p>
  42.   <p class="PropText"><help:paragraphinfo state="U" number="30" xmlns:help="http://openoffice.org/2000/help"/>Dim iCount As Integer</p>
  43.   <p class="PropText"><help:paragraphinfo state="U" number="31" xmlns:help="http://openoffice.org/2000/help"/>Dim iCount2 As Integer</p>
  44.   <p class="PropText"><help:paragraphinfo state="U" number="32" xmlns:help="http://openoffice.org/2000/help"/>Dim sTemp As String</p>
  45.   <p class="PropText"><help:paragraphinfo state="U" number="42" xmlns:help="http://openoffice.org/2000/help"/>sEntry(0) = "Jerry"</p>
  46.   <p class="PropText"><help:paragraphinfo state="U" number="33" xmlns:help="http://openoffice.org/2000/help"/>sEntry(1) = "Patty"</p>
  47.   <p class="PropText"><help:paragraphinfo state="U" number="34" xmlns:help="http://openoffice.org/2000/help"/>sEntry(2) = "Kurt"</p>
  48.   <p class="PropText"><help:paragraphinfo state="U" number="35" xmlns:help="http://openoffice.org/2000/help"/>sEntry(3) = "Thomas"</p>
  49.   <p class="PropText"><help:paragraphinfo state="U" number="36" xmlns:help="http://openoffice.org/2000/help"/>sEntry(4) = "Michael"</p>
  50.   <p class="PropText"><help:paragraphinfo state="U" number="37" xmlns:help="http://openoffice.org/2000/help"/>sEntry(5) = "David"</p>
  51.   <p class="PropText"><help:paragraphinfo state="U" number="38" xmlns:help="http://openoffice.org/2000/help"/>sEntry(6) = "Cathy"</p>
  52.   <p class="PropText"><help:paragraphinfo state="U" number="39" xmlns:help="http://openoffice.org/2000/help"/>sEntry(7) = "Susie"</p>
  53.   <p class="PropText"><help:paragraphinfo state="U" number="40" xmlns:help="http://openoffice.org/2000/help"/>sEntry(8) = "Edward"</p>
  54.   <p class="PropText"><help:paragraphinfo state="U" number="41" xmlns:help="http://openoffice.org/2000/help"/>sEntry(9) = "Christine"</p>
  55.   <p class="PropText"><help:paragraphinfo state="U" number="44" xmlns:help="http://openoffice.org/2000/help"/>For iCount = 0 To 9</p>
  56.   <p class="PropText"><help:paragraphinfo state="U" number="45" xmlns:help="http://openoffice.org/2000/help"/>For iCount2 = iCount + 1 To 9</p>
  57.   <p class="PropText"><help:paragraphinfo state="U" number="46" xmlns:help="http://openoffice.org/2000/help"/>If sEntry(iCount) > sEntry(iCount2) Then</p>
  58.   <p class="PropText"><help:paragraphinfo state="U" number="47" xmlns:help="http://openoffice.org/2000/help"/>sTemp = sEntry(iCount)</p>
  59.   <p class="PropText"><help:paragraphinfo state="U" number="48" xmlns:help="http://openoffice.org/2000/help"/>sEntry(iCount) = sEntry(iCount2)</p>
  60.   <p class="PropText"><help:paragraphinfo state="U" number="49" xmlns:help="http://openoffice.org/2000/help"/>sEntry(iCount2) = sTemp</p>
  61.   <p class="PropText"><help:paragraphinfo state="U" number="50" xmlns:help="http://openoffice.org/2000/help"/>End If</p>
  62.   <p class="PropText"><help:paragraphinfo state="U" number="51" xmlns:help="http://openoffice.org/2000/help"/>Next iCount2</p>
  63.   <p class="PropText"><help:paragraphinfo state="U" number="52" xmlns:help="http://openoffice.org/2000/help"/>Next iCount</p>
  64.   <p class="PropText"><help:paragraphinfo state="U" number="57" xmlns:help="http://openoffice.org/2000/help"/>For iCount = 0 To 9</p>
  65.   <p class="PropText"><help:paragraphinfo state="U" number="58" xmlns:help="http://openoffice.org/2000/help"/>Print sEntry(iCount)</p>
  66.   <p class="PropText"><help:paragraphinfo state="U" number="59" xmlns:help="http://openoffice.org/2000/help"/>Next iCount</p>
  67.   <p class="PropText"><help:paragraphinfo state="U" number="60" xmlns:help="http://openoffice.org/2000/help"/>End Sub</p>
  68.  </body></html>